home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 4_0 / STANDALO / SHOWINIT / INIT_ICO.C < prev    next >
Text File  |  1990-07-29  |  5KB  |  161 lines

  1. /*
  2.     init_icon.c : Shows the icon during the init loading time
  3. */
  4.  
  5. /*
  6.     Note: This code is roughly based on an assembly language routine by
  7.         Paul Mercer, Darin Adler, and Paul Snively from an idea by Steve Capps.
  8.         We use this method to be compatible with other Mac INITs.
  9.         
  10.         It was converted to LightSpeed C by Eric Shapiro.
  11. */
  12.  
  13.     /* The low memory CurApName is a convenient place to store
  14.         four bytes of information so the next INIT doesn't
  15.         overwrite our ICON.  We store into my_h the horizontal
  16.         pixel location where the next INIT should go.  We checksum
  17.         the value and store the checksum in the next two bytes,
  18.         so that INITs can determine whether they're the 1st one loaded.
  19.         ( The notation is very Think C specific }
  20.     */
  21. #include <Color.h>
  22.  
  23. extern     short        my_h : CurApName+32-4;    
  24. extern        short        my_check : CurApName+32-2;
  25.  
  26. #ifndef NULL
  27. #define NULL ( (void *)0 )
  28. #endif
  29.  
  30. #define     ICON_HEIGHT        32
  31. #define     ICON_WIDTH        32
  32. #define    FIRST_X        8
  33. #define    BOTTOM_MARGIN        8
  34. #define     DEF_MOVE_X_BY        40
  35. #define    CHECKSUM_CONST    0x1021
  36. #define     MIN_BIT_DEPTH        4    /* if 16 colors or more, use color icon */
  37.  
  38.  
  39. /*
  40.     show_init_icon: Shows the icon specified in the correct place during startup
  41.     
  42.     NOTE:
  43.         If Color QDraw is present and the main device has at least MIN_BIT_DEPTH
  44.         set, we'll look for a color icon ('cicn' resource) first. If we can't
  45.         find one, we'll look for the 'ICN#' resource.
  46.  
  47.         Passed:
  48.             short    icon_num;        'ICN#' or 'cicn' resource id
  49.             short    move_x_by;        Pass -1 for default
  50.             
  51.         Returns:
  52.             int        0=No error, <0=Error number
  53. */
  54. show_init_icon(icon_num, move_x_by)
  55.     short icon_num;
  56.     short move_x_by;
  57. {
  58.     short        err;
  59.     Handle        icon_h=NULL;        /* handle to 'ICN#' or 'cicn' resource */
  60.     Boolean    in_color=FALSE;    /* TRUE='cicn' resource, else 'ICN#' */
  61.     GrafPtr    oldport;
  62.     GrafPort    newport;        /* Entire GrafPort structure */
  63.     Rect         r;            /* where to put the icon */
  64.     SysEnvRec    sys_rec;
  65.     
  66.         /* check for color quickdraw. If present, check bit depth of mainscreen.
  67.             if >= MIN_BIT_DEPTH, then try to use color icon.
  68.         */
  69.     if ( err = SysEnvirons(1, &sys_rec) )
  70.         return(err);
  71.     if ( sys_rec.hasColorQD )        /* a Mac II or later... */
  72.     {
  73.         GDHandle gdev = GetGDevice();    /* get main screen */
  74.         if ( (*(*gdev)->gdPMap)->pixelSize >= MIN_BIT_DEPTH )
  75.             icon_h = (Handle)GetCIcon(icon_num);
  76.         if ( icon_h != NULL )
  77.             in_color = TRUE;
  78.     }
  79.     
  80.     if ( icon_h == NULL )        /* no icon yet, try a B&W one */
  81.         icon_h = Get1Resource('ICN#', icon_num);
  82.     if ( icon_h == NULL )
  83.         return(resNotFound);    /* we're SOL here */
  84.  
  85.     GetPort(&oldport);        /* save old GrafPort for later restoring */
  86.  
  87.         /* open a port so we can draw directly to screen. WindowMgrPort
  88.             may not be initialized at this time!
  89.         */
  90.     OpenPort(&newport);
  91.     SetPort(&newport);
  92.     
  93.         /* do a checksum on low memory to see if we're the 1st icon */
  94.         /* if we are, then set x location. ^=XOR. */
  95.     if ( ((my_h <<1) ^ CHECKSUM_CONST) != my_check )
  96.         my_h = FIRST_X;
  97.         
  98.         /* now plot the icon */
  99.     r = newport.portRect;
  100.     r.top = newport.portRect.bottom - (BOTTOM_MARGIN+ICON_HEIGHT);
  101.     r.bottom = newport.portRect.bottom - BOTTOM_MARGIN;
  102.     r.left = my_h;
  103.     r.right = r.left + ICON_WIDTH;
  104.     if ( in_color )
  105.         PlotCIcon(&r, icon_h);        /* Toolbox's routine */
  106.     else
  107.         plot_icn(&r, icon_h);        /* our routine */
  108.     
  109.         /* now offset the low memory location by move_x_by pixels */
  110.     if ( move_x_by == -1 )
  111.         move_x_by = DEF_MOVE_X_BY;
  112.  
  113.     my_h += move_x_by;
  114.     my_check = (my_h<<1) ^ CHECKSUM_CONST;
  115.  
  116.         /* release the icon, restore the port */
  117.     if ( in_color )
  118.         DisposCIcon(icon_h);
  119.     else
  120.         ReleaseResource(icon_h);
  121.     SetPort(oldport);
  122.     ClosePort(&newport);                /* deallocate port structures */
  123.     return(0);
  124. }
  125.  
  126.  
  127. /*
  128.     plot_icn : Plots an ICN# (ICON+mask) just the the Finder.
  129. */
  130. plot_icn(r, icn_handle)
  131.     Rect    *r;
  132.     Handle    icn_handle;
  133. {
  134.     BitMap        bm;
  135.     char        flags;
  136.     GrafPtr        cur_port;
  137.     
  138.     flags = HGetState(icn_handle);
  139.     HLock(icn_handle);
  140.     GetPort(&cur_port);
  141.     
  142.         /* we have to set up a BitMap structure to pass to CopyBits. We
  143.             might be able to use CopyMask() call instead, but this should
  144.             work on all Macs and CopyMask is only Mac+ or later. The 1st
  145.             BitMap we use has baseAddr pointing 128 bytes into the ICN#,
  146.             at the 1st bit of the mask. The 2nd BitMap points to the ICON.
  147.         */
  148.     bm.baseAddr = *icn_handle + ICON_WIDTH*ICON_HEIGHT/8;    /* points to mask */
  149.     bm.rowBytes = ICON_WIDTH / 8;    /* ICON_WIDTH/8 must be power of 2 */
  150.     SetRect(&bm.bounds, 0, 0, ICON_WIDTH, ICON_HEIGHT);
  151.  
  152.         /* punch a hole in the Desktop using bit-clear mode and the mask */
  153.     CopyBits(&bm, &cur_port->portBits, &bm.bounds, r, srcBic, NULL);
  154.  
  155.         /* now copy the icon into the punched hole */
  156.     bm.baseAddr = *icn_handle;        /* points to ICON */
  157.     CopyBits(&bm, &cur_port->portBits, &bm.bounds, r, srcOr, NULL);
  158.     
  159.     HSetState(icn_handle, flags);
  160. }
  161.